home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / CBGRX103.ZIP / contrib / libgrx / src / fileutil.c < prev    next >
Text File  |  1993-12-06  |  3KB  |  118 lines

  1. /** 
  2.  ** FILEUTIL.C 
  3.  **
  4.  **  Copyright (C) 1992, Csaba Biegl
  5.  **    820 Stirrup Dr, Nashville, TN, 37221
  6.  **    csaba@vuse.vanderbilt.edu
  7.  **
  8.  **  This file is distributed under the terms listed in the document
  9.  **  "copying.cb", available from the author at the address above.
  10.  **  A copy of "copying.cb" should accompany this file; if not, a copy
  11.  **  should be available from where this file was obtained.  This file
  12.  **  may not be distributed without a verbatim copy of "copying.cb".
  13.  **  You should also have received a copy of the GNU General Public
  14.  **  License along with this program (it is in the file "copying");
  15.  **  if not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  16.  **  Cambridge, MA 02139, USA.
  17.  **
  18.  **  This program is distributed in the hope that it will be useful,
  19.  **  but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.  **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.  **  GNU General Public License for more details.
  22.  **/
  23.  
  24. #include "grx.h"
  25. #include "libgrx.h"
  26. #include "grxfile.h"
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <ctype.h>
  30. #include <stdio.h>
  31. #include <fcntl.h>
  32. #include <io.h>
  33.  
  34. void _GrSetPath(char *name,char *pathbuffer)
  35. {
  36.     char *p;
  37.  
  38.     if(name != NULL) {
  39.         for(p = name; isspace(*p); p++);
  40.         if(*p != '\0') {
  41.         strcpy(pathbuffer,p);
  42.         for(p = pathbuffer; (*p != '\0') && !isspace(*p); p++)
  43.             *p = (*p == '\\') ? '/' : tolower(*p);
  44.         if(p[-1] != '/')
  45.             *p++ = '/';
  46.         *p = '\0';
  47.         return;
  48.         }
  49.     }
  50.     strcpy(pathbuffer,"./");
  51. }
  52.  
  53. char *_GrGetFname(char *name,char *path,char *env,char *ext,char *where)
  54. {
  55.     char temp[200];
  56.     char *p;
  57.  
  58.     where[0] = '\0';
  59.     for(p = name; isspace(*p); p++);
  60.     strcpy(temp,p);
  61.     for(p = temp; (*p != '\0') && !isspace(*p); p++)
  62.         *p = (*p == '\\') ? '/' : tolower(*p);
  63.     *p = '\0';
  64.     if((ext != NULL) &&
  65.        (((p = strrchr(temp,'.')) == NULL) || (strchr(p,'/') != NULL)))
  66.         strcat(temp,ext);
  67.     if((strchr("./",temp[0]) == NULL) && (temp[1] != ':')) {
  68.         if(path[0] == '\0') {
  69.         p = ((env != NULL) && (env[0] != '\0')) ? getenv(env) : NULL;
  70.         _GrSetPath(p,path);
  71.         }
  72.         strcpy(where,path);
  73.     }
  74.     strcat(where,temp);
  75.     return(where);
  76. }
  77.  
  78. int _GrFileOpen(char *name)
  79. {
  80.     return(open(name,(O_RDONLY | O_BINARY)));
  81. }
  82.  
  83. void _GrFileClose(int handle)
  84. {
  85.     close(handle);
  86. }
  87.  
  88. #ifdef __TURBOC__
  89.  
  90. #include <dos.h>
  91.  
  92. long _GrFarRead(int file,void far *bp,long size)
  93. {
  94.     char b[1024];
  95.     long total = 0;
  96.     int  step,test;
  97.  
  98.     while(size > 0L) {
  99.         if((long)(step = 1024) > size) step = (int)size;
  100.         if((test = read(file,b,step)) != step) {
  101.         if((step = test) < 0) {
  102.             step = 0;
  103.             if(total == 0L) total = -1L;
  104.         }
  105.         size = 0L;
  106.         }
  107.         if(step) movedata(FP_SEG(b),FP_OFF(b),FP_SEG(bp),FP_OFF(bp),step);
  108.         bp = (void far *)((char huge *)bp + (long)step);
  109.         total += (long)step;
  110.         size  -= (long)step;
  111.     }
  112.     return(total);
  113. }
  114.  
  115. #endif
  116.  
  117.  
  118.